export default {
    // 开启命名空间
    namespaced: true,
    // 用来存放数据的
    state: {
        historyArray: [],
        user: {},
        FavoritesList: [],
        CommentList: [],
        userInfo: {}
    },
    // 对state的一个封装, 可以认为是state的计算属性
    getters: {},
    // 主要用来处理业务, 比如定时器 发送网络请求
    actions: {
        // 获取历史记录列表
        getHistoryList({ commit, rootState }) {
            const { hostname, urlImg, comment: { userId } } = rootState.moduleCommon
            uni.request({
                url: `${hostname}/api/browse/history/${userId}`,
                success: res => {
                    commit('getHistoryList', { 'posts': res.data.data, 'urlImg': urlImg })
                },
                fail: err => {
                    console.log(err);
                }
            })
            // console.log('触发请求');
            // console.log('context', context);
        },
        // 获取收藏记录列表
        getFavoritesList({ commit, rootState }) {
            const { hostname, urlImg, comment: { userId } } = rootState.moduleCommon
            uni.request({
                url: `${hostname}/api/browse/collect/${userId}`,
                success: res => {
                    commit('getFavoritesList', { 'posts': res.data.data, urlImg })
                },
                fail: err => {
                    console.log(err);
                }
            })
        },
        // 获取打卡列表
        getCommentList({ commit, rootState }) {
            const { hostname, urlImg, comment: { userId } } = rootState.moduleCommon
            uni.request({
                url: `${hostname}/api/comments/select/limit/all/${userId}`,
                success: res => {
                    commit('getCommentList', { 'posts': res.data.data, urlImg })
                },
                fail: err => {
                    console.log(err);
                }
            })
        },
        // 获取用户信息
        // http://localhost:63070/auth/oauth/check_token?token=
        getUserInfo({ commit, rootState }) {
            const { hostname, AuthorizationUrl, urlImg } = rootState.moduleCommon
            const token = uni.getStorageSync("Authorization");
            uni.request({
                url: `${hostname}/api/users/getUsers`,
                method: `post`,
                header: {
                    'Authorization': 'Bearer ' + uni.getStorageSync("Authorization")
                },
                success: res => {
                    commit("getUserInfo", { "posts": res.data.data, urlImg });
                },
                fail: err => {
                    console.log('getUserInfo', err);
                }
            })
        }

    },
    // 主要是用来处理数据, 修改数据的
    mutations: {
        // 获取历史记录列表
        getHistoryList(state, { posts, urlImg }) {
            // 给图片添加前缀
            posts.forEach(item => {
                item.foods.foodImage1 = `${urlImg}/${item.foods.foodImage1}`
                item.foods.foodImage2 = `${urlImg}/${item.foods.foodImage2}`
                item.foods.foodImage3 = `${urlImg}/${item.foods.foodImage3}`
                item.userAvatar = `${urlImg}${item.userAvatar}`
                item.collectionTime = item.collectionTime.split('T')[0]
                item.foods.foodLocation = item.foods.foodLocation.split('/')[0]
            })
            // 把数据mutate给state
            state.historyArray = posts
            state.user = posts[0]
        },
        // 获取收藏记录列表
        getFavoritesList(state, { posts, urlImg }) {
            posts.forEach(item => {
                item.foods.foodImage1 = `${urlImg}/${item.foods.foodImage1}`
                item.foods.foodImage2 = `${urlImg}/${item.foods.foodImage2}`
                item.foods.foodImage3 = `${urlImg}/${item.foods.foodImage3}`
                item.userAvatar = `${urlImg}${item.userAvatar}`
                item.collectionTime = item.collectionTime.split('T')[0]
                item.foods.foodLocation = item.foods.foodLocation.split('/')[0]
            })
            state.FavoritesList = posts
        },
        // 获取打卡列表
        getCommentList(state, { posts, urlImg }) {
            posts.forEach(item => {
                item.foods.foodImage1 = `${urlImg}/${item.foods.foodImage1}`
                item.foods.foodImage2 = `${urlImg}/${item.foods.foodImage2}`
                item.foods.foodImage3 = `${urlImg}/${item.foods.foodImage3}`
                item.userAvatar = `${urlImg}${item.userAvatar}`
                item.commentTime = item.commentTime.split(' ')[0]
                item.foods.foodLocation = item.foods.foodLocation.split('/')[0]
            })
            state.CommentList = posts
        },
        // 获得用户信息
        getUserInfo(state, { posts, urlImg }) {
            // console.log('getUserInfo', posts);
            state.userInfo = posts
            state.userInfo.userAvatar = state.userInfo.userAvatar === '' ? `${urlImg}/user/默认头像.jpg` : `${state.userInfo.userAvatar}?t=${new Date().getTime()}`
            state.userInfo.userBackground = state.userInfo.userBackground === '' ? `${urlImg}/user/默认背景.jpg` : `${state.userInfo.userBackground}?t=${new Date().getTime()}`
            // state.moduleCommon.comment.userId = userId
            // console.log('userInfo', state.userInfo);
        },
    }
}